<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息ID" prop="messageId">
        <el-input
          v-model="queryParams.messageId"
          placeholder="请输入消息ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接收用户ID" prop="receiverId" label-width="auto">
        <el-input
          v-model="queryParams.receiverId"
          placeholder="请输入接收用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ido:message_receive:add']"
        >发布
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete-solid"
          size="mini"
          @click="handleWithdraw"
          v-hasPermi="['ido:message_receive:remove']"
        >撤回
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ido:message_receive:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ido:message_receive:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ido:message_receive:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="message_receiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="消息ID" align="center" prop="messageId"/>
      <el-table-column label="接收用户ID" align="center" prop="receiverId"/>
      <el-table-column label="阅读状态" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ido:message_receive:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ido:message_receive:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 撤回消息对话框 -->
    <el-dialog :title="title" :visible.sync="openWithdraw" width="500px" append-to-body>
      <el-form ref="formWithdraw" :model="formWithdraw" :rules="rules" label-width="90px">
        <el-form-item label="消息ID" prop="messageId">
          <el-input v-model="formWithdraw.messageId" placeholder="请输入需要撤回的消息ID"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormWithdraw">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 发布或修改消息阅读状态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="消息ID" prop="messageId">
          <el-input v-model="form.messageId" placeholder="请输入消息ID"/>
        </el-form-item>
        <el-form-item label="企业ID" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入企业ID"/>
        </el-form-item>
        <el-form-item label="地区ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入地区ID"/>
        </el-form-item>
        <el-form-item label="用户角色" prop="roleList">
          <el-select v-model="form.roleList" multiple placeholder="请输入用户角色">
            <el-option
              v-for="dict in roleOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.remark"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收用户ID" prop="receiverId">
          <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 5}" v-model="form.userIdList"
                    placeholder="请输入接收用户ID，使用符号”,“分隔多项"/>
        </el-form-item>
        <el-form-item label-width="0px">
          <div style="color:red; text-align: center">注：填写接收用户ID时请勿填写上列三项，否则会产生冲突！</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMessage_receive,
  getMessage_receive,
  delMessage_receive,
  updateMessage_receive,
  exportMessage_receive,
  sendMessage,
  withdrawMessage
} from "@/api/ido/message_receive";

export default {
  name: "Message_receive",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息阅读状态表格数据
      message_receiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 撤回按钮弹出层
      openWithdraw: false,
      // 阅读状态字典
      stateOptions: [],
      // 用户角色字典
      roleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        messageId: null,
        receiverId: null,
      },
      // 表单参数
      form: {
        messageId: null
      },
      formWithdraw: {},
      // 表单校验
      rules: {
        messageId: [
          {required: true, message: "消息ID不能为空", trigger: "blur"}
        ]
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("ido_message_receive_state").then(response => {
      this.stateOptions = response.data;
    });
    this.getDicts("ido_role").then(response => {
      this.roleOptions = response.data;
    })
  },
  methods: {
    /** 查询消息阅读状态列表 */
    getList() {
      this.loading = true;
      listMessage_receive(this.queryParams).then(response => {
        this.message_receiveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 阅读状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openWithdraw = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        messageId: null,
        enterpriseId: null,
        areaId: null,
        roleList: null,
        userIdList: null,
      };
      this.resetForm("form");
      this.formWithdraw = {
        messageId: null
      };
      this.resetForm("formWithdraw");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 发布按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "发布消息";
    },
    /** 撤回按钮操作 */
    handleWithdraw() {
      this.reset();
      this.openWithdraw = true;
      this.title = "撤回消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMessage_receive(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息阅读状态";
      });
    },
    getParams() {
      let params = {};
      params.areaId = parseInt(this.form.areaId);
      params.enterpriseId = parseInt(this.form.enterpriseId);
      params.roleList = this.form.roleList;
      if (this.form.userIdList != null) {
        let userIds = this.form.userIdList.split(/[,，]/);
        params.userIdList = [];
        for (let i = 0; i < userIds.length; i++) {
          params.userIdList.push(parseInt(userIds[i]));
        }
      }
      return params;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMessage_receive(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            /*addMessage_receive(this.form).then(response => {
              this.msgSuccess("发布成功");
              this.open = false;
              this.getList();
            });*/
            let message_id = parseInt(this.form.messageId);
            let params = this.getParams();
            sendMessage(params, message_id).then(response => {
              this.msgSuccess("发布成功");
              this.open = false;
              this.getList();
            })
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormWithdraw() {
      this.$refs["formWithdraw"].validate(valid => {
        if (valid) {
          withdrawMessage(this.formWithdraw.messageId).then(response => {
            this.msgSuccess("撤回成功");
            this.openWithdraw = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除消息阅读状态编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delMessage_receive(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有消息阅读状态数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportMessage_receive(queryParams);
      }).then(response => {
        this.download(response.msg);
      });
    }
  }
};
</script>
